#include "CFDPostOperatorsImport.h"

#include "GUIFrame/MainWindow.h"

#include "CFDPost/CFDPostGUIDialog/CFDPostFileDialogDirectory.h"
#include "CFDPost/CFDPostOperatorsInterface/CFDPostGraphEventOperator.h"
#include "CFDPost/CFDPostOperatorsGUI/CFDPostOperModelTreeMouseEvent.h"

#include "FITK_Component/FITKRenderWindowVTK/FITKGraph3DWindowVTK.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKCore/FITKThreadPool.h"

//创建后的部分
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostData.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPost3DManager.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostSteady.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostUnSteady.h"

#include "OperatorsInterface/GraphEventOperator.h"

#include "CFDPost/CFDPostOperatorsGUI/OperatorsGraphPreprocess.h"

#include <QThread>
#include <QDebug>

namespace OperModel {
// 构造函数
    CFDPostOperatorsImport::CFDPostOperatorsImport() {}
// 应用启动项
    bool CFDPostOperatorsImport::execGUI() {

        if (_senderName == "actionCFD3DManagerClear") {
            this->clear();
            return true;
        }

        GUI::MainWindow* mainWin = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());

        if (mainWin == nullptr) {
            return false;
        }

        GUI::CFDPostFileDialogDirectory* fileDialog = new GUI::CFDPostFileDialogDirectory(this, mainWin);
        fileDialog->iniFileFilterType("Support Files(*.vtk;;*.dat;;*.plt;;*.cgns)");

        //设置对话框路径
        AppFrame::FITKAppSettings* AppSetting = FITKAPP->getAppSettings();

        if (AppSetting) {
            QString workDir = AppSetting->getWorkingDir();
            QString path = AppSetting->getValue<QString>(CFDPostSelectFilePath);

            if (!path.isEmpty()) {
                fileDialog->initWin(path);

            } else if (!workDir.isEmpty()) {
                fileDialog->initWin(workDir);
            }
        }

        fileDialog->show();
        return false;
    }

    bool CFDPostOperatorsImport::execProfession() {

        //获取线程池
        Core::FITKThreadPool* pool = Core::FITKThreadPool::getInstance();

        if (pool == nullptr) {
            return false;
        }

        QString files = "";
        QString filePath = "";
        argValue("files", files);
        argValue("filePath", filePath);

        if (files.isEmpty()) {
            return false;
        }

        //存储选择路径
        AppFrame::FITKAppSettings* AppSetting = FITKAPP->getAppSettings();

        if (AppSetting) {
            AppSetting->setValue(CFDPostSelectFilePath, filePath);
        }

        QStringList fileList = files.split(";");
        for (int i = 0; i < fileList.size(); i++) {
            ImportReadThread* importThread = new ImportReadThread();
            importThread->_fileName = fileList[i];
            connect(importThread, SIGNAL(sigImportFinish(bool, int)), this, SLOT(slotImportFinish(bool, int)));
            pool->execTask(importThread);
        }
        this->clearArgs();
        return true;
    }
    bool CFDPostOperatorsImport::clear() {
        // 清理后处理 所有的数据、渲染对象
        OperGUI::OperatorsGraphPreprocess* graphOper = FITKOPERREPO->getOperatorT<OperGUI::OperatorsGraphPreprocess>("OperGraphPreprocess");//获取OperGraphPreprocess实例
        if (graphOper != nullptr) {
            graphOper->clear();
        }

        // 更新后处理结构树
        GUIOper::CFDPostOperModelTreeMouseEvent* treeOper = FITKOPERREPO->getOperatorT<GUIOper::CFDPostOperModelTreeMouseEvent>("CFDPostModelTreeEvent");//获取ModelTreeEvent实例？

        if (treeOper == nullptr) {
            return false;
        }

        treeOper->updateTree();//更新界面模型树
        return true;
    }
    // 这是一个槽函数
    void CFDPostOperatorsImport::slotImportFinish(bool isOk, int objID) {
        if (isOk == false) {
            return;    //任务导入未成功
        }

        if (objID < 0) {
            return;    //objID是无效值
        }

        // 获取文件数据
        EventOper::CFDPostGraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::CFDPostGraphEventOperator>("OperGraphPreprocess");//获取OperGraphPreprocess实例

        if (graphOper == nullptr) {
            return;
        }

        // 获取文件树的相关数据
        GUIOper::CFDPostOperModelTreeMouseEvent* treeOper = FITKOPERREPO->getOperatorT<GUIOper::CFDPostOperModelTreeMouseEvent>("CFDPostModelTreeEvent");//获取ModelTreeEvent实例？

        if (treeOper == nullptr) {
            return;
        }

        graphOper->updateSingle(objID);

        treeOper->updateTree();//更新界面模型树
    }
    void ImportReadThread::run() {
        Interface::FITKCFDPost3DManager* manager = FITKAPP->getGlobalData()->getPostData<Interface::FITKCFDPostData>()->getPostDataManager();

        if (manager == nullptr) {
            return;
        }

        //文件信息获取
        QString f(_fileName);
        f = f.replace(QString("\\"), QString("/"));
        const int index = _fileName.lastIndexOf(".");
        const int nsize = _fileName.size();
        QString suffix = _fileName.right(nsize - index - 1).toLower();
        const int lIndex = f.lastIndexOf("/");
        QString name = _fileName.right(nsize - lIndex - 1);

        bool isGroup = false;

        if (_fileName.indexOf("...") > 0) {
            isGroup = true;
        }

        Interface::FITKAbstractCFDPostData* postData = nullptr;

        if (isGroup) {
            Interface::FITKCFDPostUnSteady* data = new Interface::FITKCFDPostUnSteady(suffix, _fileName);
            QStringList fileList = data->getFiles();

            if (fileList.size() <= 0) {
                delete data;
                return;
            }

            data->setFile(fileList[0]);
            data->setDataObjectName(name);
            postData = data;

        } else {
            Interface::FITKCFDPostSteady* data = new Interface::FITKCFDPostSteady(suffix);
            data->setFile(_fileName);
            data->setDataObjectName(name);
            postData = data;
        }

        manager->appendDataObj(postData);
        // 处理接收到的数据
        qDebug() << "in thread Main thread" << QThread::currentThreadId();
        emit sigImportFinish(true, postData->getDataObjectID());
    }
}
